//
//  vdMisc.m
//  Void Dead
//
//  Created by Sidney Just on 20.11.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdMisc.h"
#import "vdKernel.h"
#import "vdTexManager.h"
#import "vdRenderer.h"
#import "vdNode.h"

NSTimeInterval timer()
{
	static NSTimeInterval lastCallTime;
	NSTimeInterval deltaTime;
	
	if(lastCallTime)
	{
		deltaTime = [NSDate timeIntervalSinceReferenceDate] - lastCallTime;
	}
	lastCallTime = [NSDate timeIntervalSinceReferenceDate];
	
	return deltaTime;
}

#pragma mark -
#pragma mark state enabling

GLenum *vdActiveStates = NULL;
int     vdActiveStatesCount;

void initEnabler()
{
	if(!vdActiveStates)
	{
		vdActiveStates = (GLenum *)malloc(sizeof(GLenum));
		vdActiveStatesCount = 0;
	}
}

void releaseEnabler()
{
	if(vdActiveStates)
	{
		free(vdActiveStates);
		vdActiveStates = NULL;
		vdActiveStatesCount = 0;
	}
}

BOOL vdEnabled(GLenum _enum)
{
	for(int i=0; i<vdActiveStatesCount; i++)
	{
		if(vdActiveStates[i] == _enum)
			return YES;
	}
	return NO;
}

void vdAddEnabled(GLenum _enum)
{
	vdActiveStatesCount += 1;
	vdActiveStates = (GLenum *)realloc(vdActiveStates, vdActiveStatesCount*sizeof(GLenum));
	vdActiveStates[vdActiveStatesCount-1] = _enum;
}

void vdRemoveEnabled(GLenum _enum)
{
	for(int i=0; i<vdActiveStatesCount; i++)
	{
		if(vdActiveStates[i] == _enum)
		{
			for(int j=i; j<vdActiveStatesCount-1; j++)
				vdActiveStates[j] = vdActiveStates[j+1];
				
				vdActiveStatesCount -= 1;
				vdActiveStates = (GLenum *)realloc(vdActiveStates, vdActiveStatesCount*sizeof(GLenum));
				
				return;
		}
	}
}

void vdEnable(GLenum _enum)
{
	if(!vdEnabled(_enum))
	{
		glEnable(_enum);
		vdAddEnabled(_enum);
	}
}

void vdDisable(GLenum _enum)
{
	if(vdEnabled(_enum))
	{
		glDisable(_enum);
		vdRemoveEnabled(_enum);
	}
}


void vdEnableClientState(GLenum _enum)
{
	if(!vdEnabled(_enum))
	{
		glEnableClientState(_enum);
		vdAddEnabled(_enum);
	}
}

void vdDisableClientState(GLenum _enum)
{
	if(vdEnabled(_enum))
	{
		glDisableClientState(_enum);
		vdRemoveEnabled(_enum);
	}
}